/*
 *@本js功能：select.js 下拉列表，输入框长度限制，时间，时间比较,验证，全选,计时,日期
 *@本js作者 杨锡君
 *@编写时间：2017年10月19日
 *@修改人：无
 *@修改时间：无
 *@修改位置：无
 */
//<div class="crumbs">
//			<div class="fl returnBox">&ensp;<img src="../images/ico_return.png" alt="返回按钮" />&ensp;<a class="looktg" href="javascript:;">返回上一页</a></div>
//			<div class="fl noticeBox"><img src="../images/ico_trumpet.png"/><div class="noticeBar"><ul><li>当前暂无消息</li></ul></div></div>
//		</div>
	var noticeCount = 0;
	var noticeObject = {
		init: function(noticeList) {
			if(noticeList.length != 0){
				$(".noticeBox ul").empty();
				for(var i = 0;i< noticeList.length;i++){
					$(".noticeBox ul").append("<li>"+ noticeList[i] +"</li>")
				}
			}
			
			var width = 0;
			var num = $(".noticeBox ul>li").length * 100;
			for(var i = 0; i < $(".noticeBox ul>li").length; i++) {
				width += $(".noticeBox ul>li").eq(i).width();
			}
			$(".noticeBox ul").width(width + num+20);

			var outWidth = $(".noticeBox ul").width();
			var timer = setInterval(function() {
				$(".noticeBox ul").css("margin-left", noticeCount--);
				if(noticeCount <= (-1)*outWidth) {
					noticeCount = $(".noticeBar").width();
				}
			}, 50);
            $(".noticeBox ul").hover(function () {
				clearInterval(timer);
            },function () {
                timer = setInterval(function() {
                    $(".noticeBox ul").css("margin-left", noticeCount--);
                    if(noticeCount <= (outWidth - 2 * outWidth)) {
                        noticeCount = $(".noticeBar").width();
                    }
                }, 50);
            });
			$(window).resize(function() {
                var outWidth = $(".noticeBox ul").width();
                clearInterval(timer);
                timer = setInterval(function() {
                    $(".noticeBox ul").css("margin-left", noticeCount--);
                    if(noticeCount <= (outWidth - 2 * outWidth)) {
                        noticeCount = $(".noticeBar").width();
                    }
                }, 50);
			});
		}
	};
	var arr = [];
	if($(".noticeBox").length!=0){
		$.ajax({
			type:"post",
			url:"getMyNoticeList",
			async:true,
			success:function(msg){
			    msg = JSON.parse(msg);
			    if(msg.result == "success"){

				}
			    if(msg.noticeList.length!=0){
                    $.map(msg.noticeList, function(opt) {
                        arr.push(opt.userName+"&ensp;:&ensp;"+opt.noticeContent);
                    });
                    noticeObject.init(arr);
                }

			}
		});
	}

//返回上一页
if($(".looktg").length!=0){
    $(".looktg").off("click").click(function () {
        var json = JSON.stringify({
            "pageName":$(this).attr("data-url")
        });
        if($(this).attr("data-departmentid")!=undefined){
            var json = JSON.stringify({
                "pageName":$(this).attr("data-url"),
                "paramData":{
                    "departmentId":$(this).attr("data-departmentid")
                }
            });
            window.location.href = "skipPage?jsonStr="+json;
        }else{
            window.location.href = "skipPage?jsonStr="+json;
        }

    })
}

window.history.go(+1);
//防止页面后退
history.pushState(null, null, document.URL);
window.addEventListener('popstate', function () {
	history.pushState(null, null, document.URL);

});
//验证
	var verification = {
		userNameCode: function(classVal) {
			var reg = /^[a-z0-9A-Z\u2E80-\u9FFF]{1,14}$/;
			if(!reg.test(classVal)) {
				return "请输入由1-14位英文，数字，汉字的姓名";
			} else {
				return true;
			}
		},
		passwordCode: function(classVal) {
			var regs = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,14}$/;
			if(!regs.test(classVal)) {
				return "请输入6-14位数字和英文组成的密码";
			} else {
				return true;
			}
		},
		stuorTeaIdcode: function(classVal) {
			var regs = /^[a-z0-9A-Z]{1,15}$/;
			if(!regs.test(classVal)) {
				return "请输入1-15位数字或英文的工号/学号";
			} else {
				return true;
			}
		},
		numberCode: function(classVal) {
			var regs = /^\d{1,}$/;
			if(!regs.test(classVal)) {
				return "请输入数字";
			} else {
				return true;
			}
		},
		numbersCode: function(classVal) {
			var regs = /^([0-9][0-9]*)+(.[0-9]{1})?$/;
			if(!regs.test(classVal)) {
				return "请正确输入分值";
			} else {
				return true;
			}
		},
		telphoneCode: function(classVal) {
			if(classVal.length!=0){
				if(!(/^1[34578]\d{9}$/.test(classVal))) {
					return "联系电话有误，请重填";
				} else {
					return true;
				}
				
			}else{
				return true;
			}
			
		},
		spaceCode: function(classVal) {
			var regy = /\s/g;
			if(regy.test(classVal)) {
				return "不能输入空格";
			} else {
				return true;
			}
		},
		specialCode: function(classVal) {
			var reg = /[\@\/\'\\\"#$%&\^\*]/g;
			if(reg.test(classVal)) {
				return "不能输入特殊字符(@/'\"#$%&^*)";
			} else {
				return true;
			}
		}

	};



	//长度控制
	var maxLengthLimit = {
		init: function(classes) {
			for(var i = 0; i < classes.length; i++) {
				$("." + classes[i]).off('keyup').keyup(function() {
					var obj = $(this);
					if(obj.val().length > obj.attr('maxlength')) {
						obj.val(obj.val().substring(0, obj.attr('maxlength')));
					}
				});
				$("." + classes[i]).off('keydown').keydown(function() {
					var obj = $(this);
					if(obj.val().length > obj.attr('maxlength')) {
						obj.val(obj.val().substring(0, obj.attr('maxlength')));
					}
				});
			}
		}
	};
	// //所有输入框加入长度限制
	// $(".publicText").attr("maxlength","200");
	// $.map($(".publicText"),function (opt) {
	// 	maxLengthLimit.init(opt);
	// });
	//
	
	var allCheck = {
		init: function(allcheckBtnId, oneCheckBtnClass) {
			//	单选和全选
			//	<input type="checkbox" name="controlAll" id="controlAll" class="allCheck"> 全选input
			//	<input   value=\"0\" id=" + opt.id + " name=\"selecteds\"  type=\"checkbox\" class=\"oneCheck\" /> 单选input
			// name属性要一致

			$("#" + allcheckBtnId + "").off("click").click(function() {

				var checklist = document.getElementsByName("selecteds");
				if(document.getElementById("controlAll").checked) {
					for(var i = 0; i < checklist.length; i++) {
						checklist[i].checked = 1;
						checklist[i].value = "1";
					}
				} else {
					for(var j = 0; j < checklist.length; j++) {
						checklist[j].checked = 0;
						checklist[j].value = "0";
					}
				}

			});
			$("." + oneCheckBtnClass + "").off("change").change(function() {

				if($(this).val() == "0") {
					$(this).val("1");
				} else {
					$(this).val("0");
				}
			});
			//		if($(".oneCheck[value=1]").length == 0) {
			//				Alert1.content = "请选择要删除的学生";
			//				Alert1.init();
			//				Alert1.show();
			//			}
		}
	};

	var timeObject = {
		timeCount: function(opt) {
			//停留时间
			var time1 = new Date().getTime();

			function toFormat1(opt) {
				var dateObj = new Date();
				var t = dateObj.getTime() - time1;
				var d = new Date(t)
				var h = d.getHours() - 8;
				var min = d.getMinutes();
				var s = d.getSeconds();
				if(h < 10) {
					h = "0" + h;
				}
				if(min < 10) {
					min = "0" + min;
				}
				if(s < 10) {
					s = "0" + s;
				}
				//	div2.innerHTML =  "停留时间1:"+h+"时"+min+"分"+s+"秒";
				opt.innerHTML = h + ":" + min + ":" + s;
			}
			//setInterval(toFormat,1);
			setInterval(toFormat1(opt), 1000);
		},
		compare: function(opt, obj) {
			//时间比较
			if(opt != null && obj != null) {
				var s1 = opt.replace(/-/g, "/");
				var s2 = obj.replace(/-/g, "/");
				d1 = new Date(s1);
				d2 = new Date(s2);

				var time = d2.getTime() - d1.getTime();
				var days = parseInt(time / (1000 * 60 * 60 * 24));
				if(days > 365) {
					var Alert1 = new Alert();
					Alert1.content = "查询时间不能超过一年";
					Alert1.init();
					Alert1.show();
					return false;
				} else {
					return true;
				}
			} else {
				return true;
			}
		},
		changedate: function() {
			//获取当前时间
			var datetime = new Date();

			var year = datetime.getFullYear(); //获取当前年份
			var month = datetime.getMonth() + 1; //获取当前月份
			if(month < 10)
				month = "0" + month;
			var riqi = datetime.getDate(); //获取当前日期
			if(riqi < 10)
				riqi = "0" + riqi;

			return year + "-" + month + "-" + riqi;
		},
		changedateYear:function(date){
			var datetime=new Date(date);	
			var year=datetime.getFullYear();//获取当前年份
		    var month=datetime.getMonth()+1;//获取当前月份
		    if(month<10)
		      month="0"+month;
		    var riqi=datetime.getDate();//获取当前日期
		    if(riqi<10)
		      riqi="0"+riqi;
			  
			return year+"-"+month+"-"+riqi;
		},
        changedateTime:function(date) {
            var datetime=new Date(date);
            var year=datetime.getFullYear();//获取当前年份
            var month=datetime.getMonth()+1;//获取当前月份
            if(month<10)
                month="0"+month;
            var riqi=datetime.getDate();//获取当前日期
            if(riqi<10)
                riqi="0"+riqi;
            var xs=datetime.getHours();
            if(xs<10)
                xs="0"+xs;
            var fz=datetime.getMinutes();
            if(fz<10)
                fz="0"+fz;
            var miao=datetime.getSeconds();
            if(miao<10)
                miao="0"+miao;
            return year+"-"+month+"-"+riqi+" "+xs+":"+fz+":"+miao;
        }
	};

	//时间选择器 opt为jq选择器
	//<div class="fl calendar">
	//	<div class="input-group date form_date form_date3" data-date="" data-date-format="yyyy" data-link-field="dtp_input2" data-link-format="yyyy">
	//		<input class="form-control searchStart" size="16" type="text" value="" readonly="" id="changes_ability_text" placeholder="请选择开始时间">
	//		<span class="input-group-addon"><span class="glyphicon glyphicon-remove"></span></span>
	//		<span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span></span>
	//	</div>
	//</div>
	var datePicker = {
		datePickerYear: function(opt) {
			//十年制
			opt.datetimepicker({
				language: 'zh-CN', //显示中文
				format: 'yyyy', //显示格式
				startView: "decade",
				minView: "decade", //设置只显示到月份
				initialDate: new Date(), //初始化当前日期

				autoclose: true, //选中自动关闭
				todayBtn: true //显示今日按钮
			});
		},
		datePickerMonth: function(opt) {
			//月
			opt.datetimepicker({
				language: 'zh-CN', //显示中文
				format: 'yyyy-mm-dd', //显示格式
				minView: "month", //设置只显示到月份
				initialDate: new Date(), //初始化当前日期
				autoclose: true, //选中自动关闭
				todayBtn: true //显示今日按钮
			});
		},
		datePickerTime: function(opt) {
			opt.datetimepicker({
				language: 'zh-CN', //显示中文
				format: 'yyyy-mm-dd hh:ii', //显示格式
				minView: "0", //设置只显示到月份
				initialDate: new Date(), //初始化当前日期
				autoclose: true, //选中自动关闭
				todayBtn: true //显示今日按钮
			});
		},
		datePickerTimes: function(opt) {
			opt.datetimepicker({
				language: 'zh-CN', //显示中文
				format: 'hh:ii', //显示格式
				maxView:'1',
				startView: "0",
				minView: "0", //设置只显示到月份
				// initialDate: new Date(), //初始化当前日期
                todayBtn: false,
				autoclose: true
			});
		}
	};
	//截屏
	var screenShot = {
		init: function(iframeId, canvasId, btnName, url) {
			var c = document.getElementById(iframeId).contentWindow.document.getElementById(canvasId);
			//				var c = document.getElementById("myCanvas");

			// Converts canvas to an image

			function convertCanvasToImage(canvas) {
				var image = new Image();
				image.src = canvas.toDataURL("image/png");
				return image;
			}
			var s = "";
			var clipboard;
			$(btnName).click(function() {
				$.ajax({
					type: "post",
					url: url,
					async: true,
					success: function(msg) {

						msg = JSON.parse(msg)

						s = msg[0].classID;

						clipboard = new Clipboard(btnName, {
							text: function() {
								return s;
							}
						});

					}
				});

			});
		}
	};


$(function() {
	$(".select p").click(function(e) {
		$(".select").removeClass("open");
		$(this).parent().toggleClass('open');
		e.stopPropagation();
	});

	$(" .select ul li").click(function(e) {
		var _this = $(this);
		$(this).parent().parent().find("p").text(_this.attr('data-value'));
		_this.addClass("Selected").siblings().removeClass("Selected");
		$(this).parent().parent().removeClass("open");
		e.stopPropagation();
	});

	$(document).on('click', function() {
		$(".select").removeClass("open");
	})

});
	//可是宽度
function getClientWidth() {
	var result = 0;
	if (window.innerWidth) {
		result = window.innerWidth;
	} else {
		//检查一下当前浏览器的模式
		//标准模式：CSS1Compat    怪癖模式："BackCompat"
		if (document.compatMode == "CSS1Compat") {
			//标准模式
			result = document.documentElement.clientWidth;
		} else {
			result = document.body.clientWidth;
		}
	}
	return result;
}
//可是高度
function getClientHeight() {
	var result = 0;
	if (window.innerHeight) {
		result = window.innerHeight;
	} else {//检查一下当前浏览器的模式
//标准模式：CSS1Compat    怪癖模式："BackCompat"
if(document.compatMode == "CSS1Compat") {
	//标准模式
	result = document.documentElement.clientHeight;
} else {
	result = document.body.clientHeight;
}}
	return result;
}
//获取路劲
function getImgURL(node) {
		
		var nodeVal = node.value.substring(node.value.lastIndexOf(".")+1).toLowerCase();
		
		var imgURL = "";
		try {
			var file = null;
			if(node.files && node.files[0]) {
				file = node.files[0];
			} else if(node.files && node.files.item(0)) {
				file = node.files.item(0);
			}
			
			//Firefox 因安全性问题已无法直接通过input[file].value 获取完整的文件路径  
			try {
				//Firefox7.0   
				imgURL = file.getAsDataURL();
			} catch(e) {
				//Firefox8.0以上                                
				imgURL = window.URL.createObjectURL(file);
			}
		} catch(e) { //这里不知道怎么处理了，如果是遨游的话会报这个异常                   
			//支持html5的浏览器,比如高版本的firefox、chrome、ie10  
			if(node.files && node.files[0]) {
				var reader = new FileReader();
				reader.onload = function(e) {
					imgURL = e.target.result;
				};
				reader.readAsDataURL(node.files[0]);
			}
		}
//		if(nodeVal != "jpg"&&nodeVal != "jpeg"&&nodeVal != "png"){
//			imgURL = "images/pc/fileShow.jpg";
//		}
		return imgURL;
}


//时间超时提示功能
var AlertMsg = new Alert;
AlertMsg.contenthead = "提示";

$(document).ajaxSuccess(function(a,b,c){
	
	var msgNew = JSON.parse(b.responseText);
	if(msgNew.logTimeOut== "fail") {
		
	    if(msgNew.failMsg){
	    	$(".alert").hide();
			AlertMsg.content = msgNew.failMsg;
			AlertMsg.success = function(){
				window.location.href = "logOut";
			};
			AlertMsg.init();
			AlertMsg.show();
			b.abort();
			return;
        }
		
	};
    
});
